import java.util.ArrayList;

public class SJFScheduler extends AbstractScheduler {
	public SJFScheduler(ArrayList<Process> queue) {
		super(queue);
	}

	@Override
	public ScheduleProcess getProcess() {
		//
		if(super.queue.isEmpty()){
			return null;
		}
		
		Process smallestProcess = null;

		for (int i = 0; i < super.queue.size(); i++) {
			if (smallestProcess == null) {
				smallestProcess = super.queue.get(i);
			} else if (super.queue.get(i).getNextSetOfCPUCycles() < smallestProcess
					.getNextSetOfCPUCycles()) {
				smallestProcess = super.queue.get(i);
			}
		}

		return new ScheduleProcess(smallestProcess, smallestProcess.numCycles());
	}
	
	public static void main(String[] args){
		ArrayList<Process> queue = new ArrayList<Process>();
		IScheduler scheduler = new SJFScheduler(queue);
		ArrayList<ProcessArrival> processArrivalList = new ReadProcesses().read("TestProcesses.txt");
		
		for(ProcessArrival thisArrival: processArrivalList){
			queue.add(thisArrival.getProcess());
		}
		boolean firstTime = true;
		while(queue.size() > 0){
			Process localProcess = scheduler.getProcess().Process;
			System.out.println(queue.indexOf(localProcess));
			queue.remove(localProcess);
			
			if(queue.size() == 1 && firstTime){
				firstTime = false;
				localProcess = new Process();
				localProcess.addCycle(new CPUCycle());
				queue.add(localProcess);
			}
		}
	}
}
